#!/usr/bin/env python

import getopt,sys
import subprocess
from random import random,randint,seed
from math import sqrt

seed(123) # defino el seed para hacer el experimento reproducible

def runtest(li,args):
	# abro ./domino con parametros args
	fp=subprocess.Popen(['./domino']+args, shell=False, stdin=subprocess.PIPE,stdout=subprocess.PIPE)
	for caso,p in li:
		fp.stdin.write(str(len(caso))+"\n") # le mando el tamanio
		for line in caso:
			fp.stdin.write(line+"\n") # le mando la linea de la matriz
		r=fp.stdout.readline().rstrip() # recibo el resultado
		print "n=",len(caso)," p=",p," ",args[0],"=",r
		yield len(caso),p,r # lo devuelvo
	fp.stdin.write("-1\n") # le mando un -1 para que termine

def runarchivo(archivo,args):
	# abro ./domino con parametros args
	fp=subprocess.Popen(['./domino']+args, shell=False, stdin=subprocess.PIPE,stdout=subprocess.PIPE)
	fin=open(archivo)
	while 1:
		sze=int(fin.readline().rstrip())
		if sze<0: break
		fp.stdin.write(str(sze)+"\n") # le mando el tamanio
		for line in range(sze):
			fp.stdin.write(fin.readline()) # le mando la linea de la matriz
		r=fp.stdout.readline().rstrip() # recibo el resultado
		print "n=",sze," ",args[0],"=",r
		yield sze,r # lo devuelvo
	fp.stdin.write("-1\n") # le mando un -1 para que termine


def int2matrix(n,l):
	res=''
	while n>1:
		res=(n%2 and '#' or '_') + res
		n/=2
	res=(n and '#' or '_') + res
	return [('_'*(l**2-len(res)) + res)[i:i+l] for i in range(0,l**2,l)]

def randMatrix(n,p): #una matriz al azar con probabilidad p de que cada casilla sea sana
	res=[]
	for i in range(n):
		res.append('')
		for j in range(n):
			res[i]=(random()<p and '#' or '_') + res[i]
	return res


def usage():
	print "Usage:"
	print "-t (o --time) para calcular tiempos"
	print "-c (o --count) para contar operaciones"
	sys.exit(2)

try:
	opts, args = getopt.getopt(sys.argv[1:], "tc", ["time", "count"])
except getopt.GetoptError, err:
	# print help information and exit:
	print str(err) # will print something like "option -a not recognized"
	usage()
if not opts: usage()
opts=map(lambda x: x[0],opts)


# genero la lista de los casos de prueba
ms=[(randMatrix(n,p/20.),p/20.) for n in range(1,9) for p in range(1,20,1) for re in range(15)]

out=""
if "--time" in opts or "-t" in opts:
	for i in runarchivo("test_sanos.in",['time','0.003','3']): # para cada caso de prueba...
		vals=map(float,i[1].split()) # obtengo la lista de los tiempos
		val=reduce(lambda x,y:x+y,vals)/len(vals) # tomo el promedio
		err=max([abs(x-val) for x in vals]) # calculo el error
		dat = (str(i[0])+"\t" + "%.8f"%val +"\t"+ "%.8f"%err +"\n") # lo preparo para el archivo de salida
		out += dat
	open('time_err_sanos.dat','w').write("# N\tP\tTIEMPO\tERROR\n"+out) # lo guardo en el archivo
	print "\nOutput escrito a time_err_sanos.dat"

out=""
if "--count" in opts or "-c" in opts:
	for i in runarchivo("test_sanos.in",['count']): #para cada caso de prueba..
		dat = str(i[0])+"\t"+"\t"+i[1]+"\n" # lo formateo para la salida
		out += dat
	open('counts_sanos.dat','w').write("# N\tP\tCOUNT\n"+out) # lo guardo en el archivo
	print "\nOutput escrito a counts_sanos.dat"

out=""
notli=[303,305,318,325,328,331,338,360,362,363,370,374,376,383,384,385,391,393,397,400,403,411,414,415,417,423,425,428,431,436,438,441,442,450,452,457,458,459,462,466,470,471,473,474,475,481,485,491,492,496,498,500,505,507,511,512,513,514,515,516,518,519,522,524,525,526,527,529,531,532,534,536,537,538,539,540,541,542,546,548,550,553,554,556,557,558,559,560,562,563,564,565,566,567,568,569,594,605,606,607,618,626,629,639,649,650,651,653,656,659,670,675,680,685,687,696,700,703,704,711,722,729,731,735,742,745,746,752,753,754,756,758,769,773,776,779,786,790,792,793,794,801,802,803,806,812,815,817,821,834,841,849,851,859,880,882,886,892,894,895,900,901,908,918,923,925,926,931,938,941,948,954,961,967,971,975,979,988,991,993,994,1002,1007,1008,1012,1017,1021,1023,1038,1041,1042,1045,1050,1056,1060,1062,1063,1075,1086,1101,1102,1106,1110,1111,1112,1120,1122,1127,1129,1131,1132,1133,1134,1135,1136,1137,1138,1141,1144,1153,1156,1164,1165,1167,1168,1177,1183,1184,1185,1189,1193,1194,1201,1214,1221,1231,1239,1260,1275,1285,1286,1294,1308,1310,1326,1327,1344,1347,1356,1362,1379,1380,1390,1415,1418,1420,1426,1428,1429,1431,1432,1437,1447,1456,1460,1464,1469,1475,1481,1488,1497,1503,1513,1515,1527,1529,1532,1534,1538,1555,1579,1584,1622,1648,1649,1671,1677,1678,1681,1688,1692,1695,1700,1701,1706,1707,1713,1715,1716,1721,1734,1735,1738,1750,1751,1762,1765,1777,1779,1793,1794,1808,1829,1830,1837,1843,1846,1850,1856,1882,1884,1889,1890,1908,1912,1917,1919,1934,1938,1939,1943,1958,1968,1976,1977,1981,1992,1994,1995,1999,2002,2003,2009,2027,2035,2039,2040,2042,2048,2066,2071,2079,2080,2081,2086,2109,2113,2130,2131,2137,2139,2141,2150,2151,2166,2175,2176,2178,2192,2199,2200,2208,2213,2232,2246,2263,2265,2266,2273,2277,2278,2279]
for m in range(len(ms)):
	if m not in notli:
		out+=str(len(ms[m][0]))+"\n"
		for line in ms[m][0]:
			out+=line+"\n"
open('test_imposibles.in','w').write(out+"-1\n")
